package database;

import java.text.SimpleDateFormat;

import util.FileUtil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

@SuppressLint("SdCardPath")
public class DatabaseHelper extends SQLiteOpenHelper {

	private static final String DATABASE_PATH = "/sdcard/ACD/DB/";
	private static final String DATABASE_FILE = "malaria_acd";
	private static final String DATABASE_NAME = DATABASE_PATH+DATABASE_FILE;
	private static final int DATABASE_VERSION = 1;
	
	/**  table  name  ***/  
	private static final String DATABASE_TABLE_tbl_ms_province = "tbl_ms_province";
	private static final String DATABASE_TABLE_tbl_ms_district = "tbl_ms_district";
	private static final String DATABASE_TABLE_tbl_ms_subdistrict = "tbl_ms_subdistrict";
	private static final String DATABASE_TABLE_tbl_user = "tbl_user";
	private static final String DATABASE_TABLE_tbl_acd = "tbl_acd";
	@SuppressWarnings("unused")
	private static Context cont;
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	
	
	/*** table  columns  ***/
	
	

	/***create table ***/
			private static final String DATABASE_CREATE_TABLE_tbl_ms_province =" "
			+" CREATE TABLE "+DATABASE_TABLE_tbl_ms_province+" (  "
			+" PROVINCE_ID nvarchar(10) "
			+", PROVINCE_NAME_TH nvarchar(50) "
			+", PROVINCE_NAME_EN nvarchar(50) "
			+");";
		
		private static final String DATABASE_CREATE_TABLE_tbl_ms_district =" "
			+" CREATE TABLE "+DATABASE_TABLE_tbl_ms_district+" (  "
			+" DISTRICT_ID nvarchar(10) "
			+", PROVINCE_ID nvarchar(10) "
			+", DISTRICT_NAME_TH nvarchar(50) "
			+", DISTRICT_NAME_EN nvarchar(50) "
			+");";
		
		private static final String DATABASE_CREATE_TABLE_tbl_ms_subdistrict =" "
			+" CREATE TABLE "+DATABASE_TABLE_tbl_ms_subdistrict+" (  "
			+" SUBDISTRICT_ID nvarchar(10) "
			+", DISTRICT_ID nvarchar(10) "
			+", SUBDISTRICT_NAME_TH nvarchar(50) "
			+", SUBDISTRICT_NAME_EN nvarchar(50) "
			+");";
		
		private static final String DATABASE_CREATE_TABLE_tbl_user =" "
			+" CREATE TABLE "+DATABASE_TABLE_tbl_user+" (  "
			+" user_type nvarchar(5) "
			+", user_province_id nvarchar(5) "
			+", user_sync_date nvarchar(20) "
			+");";
		
		private static final String DATABASE_CREATE_TABLE_tbl_acd =" "
				+" CREATE TABLE "+DATABASE_TABLE_tbl_acd+" (  "
				+" acd_id nvarchar(10) "
				+", blood_film_order nvarchar(10) "
				+", blood_test_type_code  nvarchar(2) " +
				" , site_id nvarchar(10)" +
				" ,treatment_place_id navarchar(10)" +
				" ,area_code nvarchar(2)" +
				" ,blood_draw_date nvarchar(10)" +
				" ,blood_film_received_date nvarchar(10)"+
				" ,blood_film_receiver_id nvarchar(10)" +
				" ,blood_film_verified_date nvarchar(10)" +
				" ,blood_film_verification_id nvarchar(10)" +
				" ,patient_type_code nvarchar(10)" +
				" ,patient_people_type nvarchar(10)" +
				" ,patient_nation_id nvarchar(10)" +
				" ,patient_prefix nvarchar(10)" +
				" ,patient_fname nvarchar(10)" +
				" ,patient_lname nvarchar(10) " +
				" ,patient_gender_code nvarchar(10)" +
				" ,patient_occupation nvarchar(10)" +
				" ,patient_site_id nvarchar(10)" +
				" ,patient_is_m2_country nvarchar(10)" +
				" ,blood_test_result_code nvarchar(10)" +
				" ,patient_lattitude nvarchar(10)" +
				" ,patient_longitude nvarchar(10)" +
				" ,patient_fname_type nvarchar(10)" +
				" ,patient_lname_type nvarchar(10)" +
				" ,patient_housenumber_type nvarchar(10)" +
				" ,patient_fname_text nvarchar(10)" +
				" ,patient_lname_text nvarchar(10)" +
				" ,patient_housenumber_text nvarchar(10)" +
				" ,patient_fname_img_inpath nvarchar(10)" +
				" ,patient_lname_img_inpath nvarchar(10)" +
				" ,patient_housenumber_inpath nvarchar(10)" +
				" ,patient_fname_img_expath nvarchar(10)" +
				" ,patient_lname_img_expath nvarchar(10)" +
				" ,patient_housenumber_expath nvarchar(10)" 
				+");";
	public DatabaseHelper(Context context){	
		super(context,DATABASE_NAME,null,DATABASE_VERSION);
		cont = context;
		FileUtil.createFoldersNotExits(DATABASE_PATH);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		/*
		db.execSQL(DATABASE_CREATE_TABLE_tbl_ms_province);
		db.execSQL(DATABASE_CREATE_TABLE_tbl_ms_district);
		db.execSQL(DATABASE_CREATE_TABLE_tbl_ms_subdistrict);
		db.execSQL(DATABASE_CREATE_TABLE_tbl_user);
		db.execSQL(DATABASE_CREATE_TABLE_tbl_acd);
		
		db.execSQL("CREATE TABLE tbl_ms_odpc (odpc_id TEXT,odpc_name_th TEXT,odpc_name_en TEXT);");
		db.execSQL("CREATE TABLE tbl_ms_vbdc (odpc_id TEXT,vbdc_name_th TEXT,vbdc_name_en TEXT,vbdc_id TEXT);");
		db.execSQL("CREATE TABLE tbl_ms_vbdu (vbdu_id TEXT,vbdu_name_th TEXT,vbdu_name_en TEXT,vbdc_id TEXT);");
		db.execSQL("CREATE TABLE tbl_rpt_pos_month_2 (type_data TEXT,search_id_10 TEXT,search_id_8 TEXT,search_id_6 TEXT,search_id_4 TEXT,search_id_2 TEXT,blood_test_type TEXT,people_type TEXT,blood_test_result_detail TEXT,blood_draw_date_year_month TEXT,blood_draw_date_year TEXT,blood_draw_date_month TEXT,pos_amount TEXT,ep3_amount TEXT);");

		Log.d("CREATE TABLE","Create Table Successfully.");		
	*/
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	
	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//onUpgrade(db,oldVersion,newVersion);
	}
	
}
